package cn.iocoder.yudao.module.ad.plan.controller.app;

import cn.iocoder.yudao.framework.business.basic.controller.SimpleDeleteController;
import cn.iocoder.yudao.framework.business.basic.controller.SimpleDetailController;
import cn.iocoder.yudao.framework.business.basic.controller.UpdateController;
import cn.iocoder.yudao.framework.business.order.pojo.vo.OrderSubmitVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ad.plan.service.AppAdPlanService;
import cn.iocoder.yudao.module.ad.plan.vo.AdPlanQueryDTO;
import cn.iocoder.yudao.module.ad.plan.vo.AdPlanSaveDTO;
import cn.iocoder.yudao.module.ad.plan.vo.AppAdPlanVO;
import cn.iocoder.yudao.module.api.ad.plan.enums.AdPlanStatusEnum;
import cn.iocoder.yudao.module.api.infrastructure.config.ConfigApiV2;
import cn.iocoder.yudao.module.api.infrastructure.config.dto.AdConfigRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.api.infrastructure.ConfigConstants.CONFIG_AD;

@Tag(name = "用户 App - 广告投放计划")
@RestController
@RequestMapping("/ad/plan")
@Validated
public class AppAdPlanController implements UpdateController<AdPlanSaveDTO>, SimpleDeleteController, SimpleDetailController<AppAdPlanVO> {

    @Resource
    private AppAdPlanService appAdPlanService;

    @Resource
    private ConfigApiV2 configApiV2;

    @GetMapping("/config")
    @Operation(summary = "配置")
    public CommonResult<AdConfigRespDTO> getConfig() {
        AdConfigRespDTO config = configApiV2.getConfig(CONFIG_AD, AdConfigRespDTO.class);
        return success(config);
    }

    @PreAuthorize("@mm.isCloudAlliance()")
    @PostMapping("/submit")
    @Operation(summary = "投放", description = "广告投放记录，返回交易单号")
    public CommonResult<OrderSubmitVO> submit(@Valid @RequestBody AdPlanSaveDTO param) {
        OrderSubmitVO respVO = appAdPlanService.submit(param, getLoginUserId());
        return success(respVO);
    }

    @PreAuthorize("@mm.isCloudAlliance()")
    @Override
    public CommonResult<Boolean> update(AdPlanSaveDTO param) {
        return success(appAdPlanService.update(param, getLoginUserId()));
    }

    @PreAuthorize("@mm.isCloudAlliance()")
    @Override
    public CommonResult<Boolean> delete(Long id) {
        return success(appAdPlanService.delete(id, getLoginUserId()));
    }

    @PermitAll
    @Override
    public CommonResult<AppAdPlanVO> getDetail(Long id) {
        return success(appAdPlanService.getDetail(id));
    }

    @PreAuthorize("@mm.isCloudAlliance()")
    @Operation(summary = "我的投放记录")
    @Parameter(name = "pageNo", description = "页码", example = "1")
    @Parameter(name = "pageSize", description = "每页数据条数", example = "10")
    @GetMapping("/my-page")
    public CommonResult<PageResult<AppAdPlanVO>> getMyPage(Integer pageNo, Integer pageSize) {
        PageResult<AppAdPlanVO> page = appAdPlanService.getMyPage(pageNo, pageSize, getLoginUserId());
        return success(page);
    }

    @PermitAll
    @Operation(summary = "获取前 N 条")
    @Parameter(name = "limit", description = "数据条数", required = true, example = "1")
    @GetMapping("/top-list")
    public CommonResult<List<AppAdPlanVO>> getList(Integer limit) {
        AdPlanQueryDTO param = new AdPlanQueryDTO();
        param.setStatus(AdPlanStatusEnum.START.getValue());
        List<AppAdPlanVO> list = appAdPlanService.getList(param, limit);
        return success(list);
    }


}
